

#include "unity.h"
#include "unity_fixture.h"
#include <string.h>
#include <Debug.h>
#include <Log.h>
#include "BSP_Sleep.h"

#include "BSP.h"

/*===============================================================================*/
/*===============================================================================*/



TEST_GROUP(BSP);


TEST_SETUP(BSP)
{

}

TEST_TEAR_DOWN(BSP)
{

}


#if 1

#include "modules/uart/uart.h"

TEST(BSP, UART_TRX)
{
	BSP_Uart_DMA_Init();
	BSP_Uart_Init(2);
	uart_init();
	uint16_t rx_len;
	static uint8_t rx_buffer[128];
	
	while(1){
		rx_len = 0;
		memset(rx_buffer, 0, sizeof(rx_buffer));
		int ch = uart_get_byte(1000);
		if(ch < 0){
//				const char* timeout = "timeout\r\n";
//				uart_send((uint8_t*)timeout, strlen(timeout));
			continue;
		}
		rx_buffer[rx_len++] = ch;
		while(rx_len < sizeof(rx_buffer)){
			ch = uart_get_byte(10);
			if(ch < 0){
				break;
			}
			rx_buffer[rx_len++] = ch;
		}
		uart_send(rx_buffer, rx_len);
	}
}
#endif 

#if 0
#include "modules/usb/usb.h"
#include "drv_usb_hw.h"
#include "cdc_acm_core.h"

extern usb_core_driver cdc_acm;

TEST(BSP, USB_TRX)
{
	usb_cdc_init();
	uint16_t rx_len;
	static uint8_t rx_buffer[128];
//		while(USBD_CONFIGURED != cdc_acm.dev.cur_status){
//		
//		}
//		cdc_acm_receive_init(&cdc_acm);
	while(1){
#if 1
		rx_len = 0;
		memset(rx_buffer, 0, sizeof(rx_buffer));
		int ch = usb_cdc_get_byte(1000);
		if(ch < 0){
//				const char* timeout = "timeout\r\n";
//				uart_send((uint8_t*)timeout, strlen(timeout));
			continue;
		}
		rx_buffer[rx_len++] = ch;
		while(rx_len < sizeof(rx_buffer)){
			ch = usb_cdc_get_byte(10);
			if(ch < 0){
				break;
			}
			rx_buffer[rx_len++] = ch;
		}
		usb_cdc_data_send(rx_buffer, rx_len);
#endif 
#if 0
		BSP_Sleep_Ms(10);
		if (USBD_CONFIGURED == cdc_acm.dev.cur_status) {
			if (0U == cdc_acm_check_ready(&cdc_acm)) {
				cdc_acm_data_receive(&cdc_acm);
			} else {
				cdc_acm_data_send(&cdc_acm);
			}
		}
#else
#if 0
		BSP_Sleep_Ms(10);
		if (USBD_CONFIGURED == cdc_acm.dev.cur_status) {
			usb_cdc_handler *cdc = (usb_cdc_handler *)cdc_acm.dev.class_data[0];
			while(cdc->receive_length == 0){
				BSP_Sleep_Ms(5);
			}
			memcpy(rx_buffer, cdc->data, cdc->receive_length);
			rx_len = cdc->receive_length;
			cdc->receive_length = 0;
			while(cdc_acm_check_ready_send(&cdc_acm)){
				BSP_Sleep_Ms(5);
			}
			cdc_acm_data_send2(&cdc_acm, rx_buffer, rx_len);
		}
#endif 
#endif 
		

	}
}
#endif 



#if 0
TEST(BSP, SPI_TRX)
{
	RFIO_Init();
	
	while(1){
		BSP_Sleep_Ms(100);
		RFIO_SPI_SetNSS(0);
		for(int i = 0; i < 10; i++){
			RFIO_SPI_Exchange(0xAA);
		}
		RFIO_SPI_SetNSS(1);
	}
}
#endif 

#if 0
void test_CharacterModule(void);
TEST(BSP, SC)
{
	while(1){
		test_CharacterModule();
		BSP_Sleep_Ms(100);
	}
}
#endif 

#if 0
#include "BSP_RFID.h"

void bsp_lf_adc_phase(uint16_t pulse);

uint32_t bsp_lf_rf_period(void);

uint16_t bsp_lf_adc_value(void);

TEST(BSP, LF)
{
	BSP_RFID_Enable();
	uint32_t period = bsp_lf_rf_period();
	uint16_t pulse = 0;
	while(pulse < period){
		bsp_lf_adc_phase(pulse);
		BSP_Sleep_Ms(10);
		uint16_t val = bsp_lf_adc_value();
		LOGD(NULL, "%u\t%u", pulse, val);
		pulse += 4;
	}
	LOGW(NULL, "End");
}
#endif 


